/*
 * MIT License
 *
 * Copyright (c) 2023 北京凯特伟业科技有限公司
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.je.bpm.engine.runtime;


import com.je.bpm.engine.internal.Internal;
import com.je.bpm.engine.query.Query;

import java.util.Date;

/**
 * Allows programmatic querying of {@link Job}s.
 */
@Internal
public interface JobQuery extends Query<JobQuery, Job> {

    /**
     * Only select jobs with the given id
     */
    JobQuery jobId(String jobId);

    /**
     * Only select jobs which exist for the given process instance.
     **/
    JobQuery processInstanceId(String processInstanceId);

    /**
     * Only select jobs which exist for the given execution
     */
    JobQuery executionId(String executionId);

    /**
     * Only select jobs which exist for the given process definition id
     */
    JobQuery processDefinitionId(String processDefinitionid);

    /**
     * Only select jobs that are timers. Cannot be used together with {@link #messages()}
     */
    JobQuery timers();

    /**
     * Only select jobs that are messages. Cannot be used together with {@link #timers()}
     */
    JobQuery messages();

    /**
     * Only select jobs where the duedate is lower than the given date.
     */
    JobQuery duedateLowerThan(Date date);

    /**
     * Only select jobs where the duedate is higher then the given date.
     */
    JobQuery duedateHigherThan(Date date);

    /**
     * Only select jobs that failed due to an exception.
     */
    JobQuery withException();

    /**
     * Only select jobs that failed due to an exception with the given message.
     */
    JobQuery exceptionMessage(String exceptionMessage);

    /**
     * Only select jobs that have the given tenant id.
     */
    JobQuery jobTenantId(String tenantId);

    /**
     * Only select jobs with a tenant id like the given one.
     */
    JobQuery jobTenantIdLike(String tenantIdLike);

    /**
     * Only select jobs that do not have a tenant id.
     */
    JobQuery jobWithoutTenantId();

    /**
     * Only return jobs that are locked (i.e. they are acquired by an executor).
     */
    JobQuery locked();

    /**
     * Only return jobs that are not locked.
     */
    JobQuery unlocked();

    // sorting //////////////////////////////////////////

    /**
     * Order by job id (needs to be followed by {@link #asc()} or {@link #desc()}).
     */
    JobQuery orderByJobId();

    /**
     * Order by duedate (needs to be followed by {@link #asc()} or {@link #desc()}).
     */
    JobQuery orderByJobDuedate();

    /**
     * Order by retries (needs to be followed by {@link #asc()} or {@link #desc()}).
     */
    JobQuery orderByJobRetries();

    /**
     * Order by process instance id (needs to be followed by {@link #asc()} or {@link #desc()}).
     */
    JobQuery orderByProcessInstanceId();

    /**
     * Order by execution id (needs to be followed by {@link #asc()} or {@link #desc()}).
     */
    JobQuery orderByExecutionId();

    /**
     * Order by tenant id (needs to be followed by {@link #asc()} or {@link #desc()}).
     */
    JobQuery orderByTenantId();

}
